<?php
namespace BoxEdu\Controller\Home;
//use Think\Controller;

class SubjectController extends CommonController {

	public function _initialize() {
		//BoxModel("edu_subject_order")->where(array('user_id'=>'100104'))->delete();
		//BoxModel("edu_user")->where(array('user_id'=>'100102'))->save(array('is_agent'=>1));

		parent::_initialize();
		$this->nav_cur = "Home";
		$this->subjectid = I('subjectid', "0", "intval");
		$this->subjectInfo();
		$this->tikuNav = $this->subjectInfo['tikuNavArray'] ? $this->subjectInfo['tikuNavArray'] : dict('tikuNav');
		//  $this->tikuNav = array(
		//            'tiku' => array(
		//                'text' => '课堂练习',
		//                'url' => 'Subject/tiku',
		//            ),
		//            'tiku_chapter' => array(
		//                'text' => '章节练习',
		//                'url' => 'Subject/tiku_chapter',
		//            ),
		//            'tiku_moni' => array(
		//                'text' => '全真模拟',
		//                'url' => 'Subject/tiku_moni',
		//            ),
		//            	'dotistatis'=>array(
		//																'text'=>'做题记录',
		//																'url'=>'Subject/dotistatis',
		//															),
		//															'exam_guide'=>array(
		//																'text'=>'考试指南',
		//																'url'=>'Subject/exam_guide',
		//															),
		//        );
		// dump(ACTION_NAME);
	}

	function subjectInfo() {
		if ($this->subjectid) {
			$_this_subjectInfo = $subjectInfo = BoxModel("edu_subject")->load($this->subjectid);
			if ($subjectInfo['parent_id']) {
				$subjectInfo = BoxModel("edu_subject")->load($subjectInfo['parent_id']);
				$subjectInfo['subject'] = $_this_subjectInfo['subject'];
			}
			$this->subjectParentId = $_this_subjectInfo['parent_id'] ? $_this_subjectInfo['parent_id'] : $this->subjectid;
			$subjectids = json_decode($this->user_info['subjectids'], true);
			if ($end_date = $subjectids[$this->subjectParentId]['end_date']) {
				$subjectInfo['end_date'] = $end_date;
			}
			$this->subjectInfo = $subjectInfo;

			$my_rank = BoxModel("edu_subject_rank")->where(array('subjectid' => $this->subjectid, 'user_id' => $this->user_info['user_id']))->find();
			$this->my_num = $my_num = $my_rank ? $my_rank['num'] : 0; //我的积分
			$my_rank_num = BoxModel('EduSubjectRank')->where("num>'$my_num' and subjectid='$this->subjectid'")->count(); //我的排名
			$this->my_rank_num = ($my_rank_num + 1);
			/*****学习进度****/
			$this->learning_pre = Logic("BoxEdu", "User")->learning_pre($this->subjectid);

			//dump($this->subjectInfo);

		}
	}

	/****班级首页*/
	public function index() {
		$this->subject_nav = "index";
		$this->web_title = "班级首页 - 考试突击";
//    $this->class_list=BoxModel("EduSubject")->subject_class();
		$this->display();
	}

	/****线下选课***/
	public function offline_select_course() {
		$this->web_title = "线下班级 - 考试突击";
		$this->subject_nav = "offline";
		$school_id = $this->user_info['school_id'];
		$week_array = array();
		$this->dates = Logic("BoxEdu", "Classrooms")->getDate();
		if ($this->subjectParentId != $this->subjectid) {
			$this->subjectList = BoxModel("edu_subject")->get(array('parent_id' => $this->subjectParentId));
			$subjectid = $this->subjectList[0]['subjectid'];
		} else {
			$this->subjectList = array('0' => $_this_subjectInfo);
			$subjectid = $this->subjectid;
		}
		//dump($this->subjectList);

		$param = array(
			'subjectid' => $this->subjectid,
			'school_id' => $school_id,
		);
		$data = BoxModel("edu_classrooms_subject")->get($param, "", "week");
		$model = BoxModel("edu_classrooms");
		$classrooms = array(); //可预约机房
		$timeQuantum = array(); //时间段
		foreach ($data as $k => $one) {
			$classrooms_info = $model->where(array('id' => $one['class_room_id']))->find();
			$count = Logic("BoxEdu", "Classrooms")->book_count($one['id']);
			$count = ($classrooms_info['line_num'] * $classrooms_info['row_num']) - $count;
			$one['limit_seat'] = $count;
			$count = $count > 0 ? "(剩{$count}位)" : "(满)";
			$one['class_name'] = $classrooms_info['name'];
			$one['lable'] = $one['lable'] . $count;
			$data[$k] = $one;
			$week_array[$one['week']] = $one['week_str'];
			$classrooms[$one['week']][$one['class_room_id']] = $one; //可预约机房
		}
		//dump($week_array);
		//dump($classrooms);
		$this->classrooms = $classrooms;
		$this->timeQuantum = $data;
		$this->week_array = $week_array;
		$this->display();
	}

	/****线下选课***/
	public function offline_select_seat() {
		$this->web_title = "线下班级 - 考试突击";
		$this->subject_nav = "offline";
		$id = I('id', "0", "intval");
		$data = BoxModel("edu_classrooms_subject")->where(array('id' => $id))->find();
		//	dump($data);
		$class_room_id = $data['class_room_id'];
		$model = BoxModel('EduClassrooms');
		$this->classrooms_info = $model->where(array('id' => $class_room_id))->find();
		//		dump( $this->classrooms_info);exit();
		$books = BoxModel("edu_classrooms_book")->where("part_time_id='$id' and status=1")->select();
		foreach ($books as $k => $one) {
			if ($one['user_id'] == $this->user_id) {
				$one['class'] = "has";
			} else {
				$one['class'] = "dis";
			}
			$books2[$one['seat_num']] = $one;
		}
		$this->books = $books2;
		$this->display();
	}

	/***选座记录*/
	public function offline_select_record() {
		$this->web_title = "选座记录 - 考试突击";
		$this->subject_nav = "offline";
		$where = "user_id='$this->user_id' and status=1";
		$model = BoxModel('edu_classrooms_book');
		// $count = $model->alias("S")->where($where)->count();
		// $this->setPager($count, 10);
		$subjectInfo = BoxModel("edu_subject")->load($this->subjectParentId);
		$subjectids = $subjectInfo['subjectids'];

		if ($this->subjectid) {
			$where .= " and subject_id in ($subjectids)";
		}
		$list = $model->get($where, 100);
		//dump($model);
		$data = $list['list'];
		$this->data = $data;
		$this->display();
	}

	/****课程表*****/
	public function lesson() {
		$this->subject_nav = "lesson";
		$this->web_title = "课程表 - 考试突击";
		$this->display();
	}

	/****答疑*****/
	public function question() {
		$this->subject_nav = "question";
		$this->web_title = "答疑 - 考试突击";
		$this->type = I('type', "1", "intval");
		$where = array('is_show' => 1, 'pid' => $this->subjectid);
		$typeWhere = array(
			'1' => array('is_best' => 0),
			'2' => array('is_best' => 1),
			'3' => array('user_id' => $this->user_info['user_id']),
		);
		if ($addWhere = $typeWhere[$this->type]) {
			$where = array_merge($where, $addWhere);
		}
		//	dump($where);
		$this->where = $where;
		$this->display();
	}

	/****笔记*****/
	public function note() {
		$this->web_title = "笔记 - 考试突击";
		$this->subject_nav = "note";
		$this->knowsid = I('knowsid');
		if (!$this->knowsid) {
			$this->display("Subject/note_no_knowsid");
		} else {
			$this->data = BoxModel("edu_subject_notes")->where(array('user_id' => $this->user_info['user_id'], 'knowsid' => $this->knowsid))->find();
			$this->notes = $this->data['notes'];
			//	dump($this->notes);exit();
			if (!$this->notes) {
				$data = BoxModel("edu_knows")->where(array('knowsid' => $this->knowsid))->find();
				//dump($data);exit();
				$this->notes = $data['pt_note'];
			}
			$this->display();
		}
	}

	/****题库*****/
	public function tiku() {
		$this->web_title = "题库 - 考试突击";
		$this->subject_nav = "tiku";
		$this->knowsid = I('knowsid');
		$this->has_buy($this->knowsid);
		$data = BoxModel("edu_exams")->where(array('knowsid' => $this->knowsid, 'examstatus' => 1, 'examtype' => 'knows_exam'))->find();
		$this->examid = $data['examid'];
		if (!$this->knowsid) {
			$this->display("Subject/tiku_no_knowsid");
		} else {
			$this->display();
		}
	}

	/****章节练习***/
	public function tiku_chapter() {
		$this->web_title = "章节练习 - 考试突击";
		$this->subject_nav = "tiku";
		$data = BoxModel("edu_sections")->get(array('sectionsubjectid' => $this->subjectid, 'is_show' => 1, 'status' => 1), "", "order_by");
		$exams = BoxModel("edu_exams");
		$examhistory = BoxModel("edu_examhistory");
		$questionsUser = BoxModel("edu_questions_user");
		foreach ($data as $k => $one) {
			$sectionid = $one['sectionid'];
			$data[$k]['all_exams'] = $exams->where(array('sectionid' => $sectionid, 'examstatus' => 1, 'examtype' => 'module_exam'))->count();
			$data[$k]['done_exams'] = $examhistory->where(array("user_id" => $this->user_info['user_id'], 'sectionid' => $sectionid))->count();
			$data[$k]['favorite'] = $questionsUser->where(array("user_id" => $this->user_info['user_id'], 'sections_id' => $sectionid, 'type' => 'favorite'))->count();
			$data[$k]['wrong'] = $questionsUser->where(array("user_id" => $this->user_info['user_id'], 'sections_id' => $sectionid, 'type' => 'wrong'))->count();
		}
		$this->list = $data;
		$this->display();
	}

	/****全真模拟***/
	public function tiku_moni() {
		$this->web_title = "全真模拟 - 考试突击";
		$this->subject_nav = "tiku";
		$exams = BoxModel("edu_exams");
		$where = "subjectid='$this->subjectid' and examtype in ('autopage','selfpage') and examstatus=1";
		$data = $exams->get($where);
		$examhistory = BoxModel('edu_examhistory');
		$questionsUser = BoxModel("edu_questions_user");
		foreach ($data as $k => $one) {
			$exam_id = $one['examid'];
			$data[$k]['historyCount'] = $examhistory->where(array('examid' => $exam_id))->count();
			$data[$k]['historyAvg'] = $examhistory->where(array('examid' => $exam_id))->avg("score");
			$data[$k]['historyAvg'] = intval($data[$k]['historyAvg']);
			$data[$k]['favorite'] = $questionsUser->where(array("user_id" => $this->user_info['user_id'], 'exams_id' => $exam_id, 'type' => 'favorite'))->count();
			$data[$k]['wrong'] = $questionsUser->where(array("user_id" => $this->user_info['user_id'], 'exams_id' => $exam_id, 'type' => 'wrong'))->count();
			$has_done = $examhistory->where(array('examid' => $exam_id, 'user_id' => $this->user_info['user_id']))->count();
			$data[$k]['text'] = '开始模拟考';
			if ($has_done) {
				$data[$k]['text'] = '重做试题';
				$data[$k]['reload'] = 1;
			}

		}
		$this->list = $data;
		$this->display();
	}

	/****考试指南***/
	public function exam_guide() {
		$this->subject_nav = "tiku";
		$this->web_title = "考试指南 - 考试突击";
		$this->display();
	}

	/****报考指南*****/
	public function guide() {
		$this->subject_nav = "guide";
		$this->display();
	}

	/****模拟考试*/
	public function moni_info() {
		//dump($this->user_info);
		$this->web_title = "模拟考试 - 考试突击";
		$this->header_nav = "Public:none";
		$this->subject_nav = "tiku";
		$this->type = I('type');
		$this->examid = I('exam_id');
		$this->data = BoxModel("edu_exams")->load($this->examid);
		$this->getQuestype($this->data['examsetting']);
		$this->examsetting = intval($this->data['examsetting']['examtime']) * 60;
		if ($this->data['examtype'] != "module_exam") {
			$tpl = "Subject/moni_info_2";
		}
		$this->display($tpl);
	}

	/*****验证是否购买课程**/
	public function has_buy($knows_id) {

		$data = BoxModel("edu_knows")->where(array('knowsid' => $knows_id))->find();
		$subject_id = $data['subjectid'];
		$r = Logic("BoxEdu", "Subject")->has_buy($subject_id, $this->user_info['user_id']);
		if ($r['error'] > 0 && $data['type'] == 1) {
			$this->jump("需要开通课程后才能访问", U('Subject/index', array('subjectid' => $subject_id)));
			//$this->jump("需要开通课程后才能访问");
		}
	}

	public function getQuestype($examsetting) {
		$questype = array();
		foreach ($examsetting['questype'] as $one) {
			if (is_array($one['examquestions'])) {
				$examquestionsCount = count($one['examquestions']);
				$row = array(
					'name' => $one['questype']['questype'],
					'examquestionsCount' => $examquestionsCount + 1,
					'examquestions' => $one['examquestions'],
				);
				$questype[] = $row;
			}
		}
		$this->questype = $questype;
	}

	/****问题详情*/
	public function topic_info() {
		$this->topic_id = I('topic_id');
		$this->data = BoxModel("sns_topic")->where(array('id' => $this->topic_id))->find();
		$this->backLink = U('all_question');
		BoxModel("sns_topic")->hit_count($this->topic_id);
		if ($pid = $this->data['pid']) {
			$from_data = BoxModel("edu_subject")->where(array('subjectid' => $pid))->find();
			//	dump( $this->from_data);
			$data = array(
				'title' => $from_data['subject'],
				'url' => U('Subject/question', array('subjectid' => $from_data['subjectid'])),
			);
			$this->backLink = U('Subject/question', array('subjectid' => $pid));
			$widgetWhere = "pid='$pid'";
		}
		if ($pid2 = $this->data['pid2']) {
			// dump(array('sectionid' => $pid2));
			$from_data = BoxModel("edu_sections")->where(array('sectionid' => $pid2))->find();
			$data['title'] = " " . $from_data['section'];
			$widgetWhere = "pid2='$pid2'";
		}
		if ($pid3 = $this->data['pid3']) {
			$from_data = BoxModel("edu_knows")->where(array('knowsid' => $pid3))->find();
			$data['title'] = "  " . $from_data['knows'];
			$widgetWhere = "pid3='$pid3'";
		}

		if ($pid4 = $this->data['pid4']) {
			$this->questionsData = BoxModel("edu_questions")->load($pid4);
			//dump(	$this->questionsData);
			$widgetWhere = "pid4='$pid4'";
		}
		//	dump($widgetWhere);
		$this->widgetWhere = $widgetWhere;
		if ($this->data['type'] == 'classNew') {
			$this->subjectid = $this->data['pid2'];
			$this->subjectInfo();
			$this->getClassNewHear();
			if ($this->data['pid'] == 2) {
				$this->subjectInfo = array(
					'picname' => '/Uploads/default/20160819/57b6b57763f36.jpg',
					'subject' => "校园动态",
				);
			}
		}
		//		dump($data);
		$this->from_data = $data;
		// dump( $this->data);
		$this->display();
	}

	public function getClassNewHear() {
		$this->where = array('pid' => 2);
		if ($this->subjectid) {
			$this->where = array('pid2' => $this->subjectid, 'pid' => 1);
		}
		$where = $this->where;
		$where['type'] = "classNew";
		$this->topic_count = BoxModel("sns_topic")->where($where)->count();
		$this->page_count = BoxModel("sns_topic")->where($where)->sum("hit_count");
		$this->comment_count = BoxModel("sns_comment")->where(array('type' => 'classNew'))->count();
	}

	public function class_new() {
		if (!$this->subjectid) {
			$this->subjectInfo = array(
				'picname' => '/Uploads/default/20160819/57b6b57763f36.jpg',
				'subject' => "校园动态",
			);
		}
		$this->getClassNewHear();
		$this->display();
	}

	//采纳回答
	public function accept() {
		$comment_id = I('comment_id', "0", "intval");
		$r = Logic("BoxEdu", "Ask")->accept($comment_id);
		echo json_encode($r);
	}

	//提问对话框
	public function ask() {
		$param = array(
			'pid' => I('pid', "0", "intval"),
			'pid2' => I('pid2', "0", "intval"),
			'pid3' => I('pid3', "0", "intval"),
		);
		//	$where=1;
		if ($param['pid']) {
			$where['pid'] = $param['pid'];
		}
		$param['where'] = $where;
		$this->param = $param;
		$this->display();
	}

	//学员心声
	public function order_comment() {

		$data = BoxModel("sns_topic")->get($p['where'], $p['limit'] ? $p['limit'] : 10, "lastupdate desc");
		//	dump(BoxModel("sns_topic"));
		$this->data = $data['list'];
		$this->count = intval($data['count']);
		$this->show_page($data['pager']);
		$param = array(
			'pid' => I('pid', "0", "intval"),
		);
		//	$where=1;
		if ($param['pid']) {
			$where['pid'] = $param['pid'];
		}
		$param['where'] = $where;
		$this->param = $param;
		$this->display();
	}

	/****所有问题***/
	public function all_question() {
		$this->type = I('type', "0", "intval");
		$where = array('is_show' => 1);
		$typeWhere = array(
			'1' => array('is_best' => 0),
			'2' => array('is_best' => 1),
			'3' => array('user_id' => $this->user_info['user_id']),
		);
		if ($addWhere = $typeWhere[$this->type]) {
			$where = array_merge($where, $addWhere);
		}
		$this->where = $where;
		$this->display();
	}

	/****课程详情***/
	public function lesson_detail() {
		$this->header_nav = "Public:header_lesson";
		$this->knowsid = I('knowsid');
		$this->has_buy($this->knowsid);
		$this->knowsData = BoxModel("edu_knows")->load($this->knowsid);
		$modelKnowsUser = BoxModel("edu_knows_user");
		if ($this->user_info['user_id']) {
			$this->knowHistory = $modelKnowsUser->where(array('user_id' => $this->user_info['user_id'], 'knowsid' => $this->knowsid))->find();
		}
		//	dump( $this->knowHistory);exit();
		$this->knowsList = BoxModel("edu_knows")->get(array('knowssectionid' => $this->knowsData['knowssectionid']), "", "order_by");
		$data = BoxModel("edu_exams")->where(array('knowsid' => $this->knowsid, 'examstatus' => 1, 'examtype' => 'knows_exam'))->find();
		$this->examid = $data['examid'];
		//笔记
		$this->data = BoxModel("edu_subject_notes")->where(array('user_id' => $this->user_info['user_id'], 'knowsid' => $this->knowsid))->find();
		$this->notes = $this->data['notes'];
		if (!$this->notes) {
			$data = BoxModel("edu_knows")->where(array('knowsid' => $this->knowsid))->find();
			$this->notes = $data['pt_note'];
		}
		// dump( $this->knowsList);
		$this->display();
	}

	/****选课程*/
	public function subject_list() {
		$subjectclass = I('subjectclass');
		$data = BoxModel("EduSubject")->get("subject_class='$subjectclass'", "20");
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->display();
	}

	/****选课程购买*/
	public function buy() {
		$subjectid = I('subjectid');
		$this->data = BoxModel("EduSubject")->load($subjectid);
		/*******线下课程**/
		$school_id = $this->user_info['school_id'];
		$param = array(
			'school_id' => $school_id,
			'subjectid' => $subjectid,
		);
		$this->offline_subject = Logic("BoxEdu", "Subject")->get_offline_subject_price($param);
		$this->display();
	}

	/******获得优惠码***/
	public function get_card() {
		$num = I('num');
		$type = I('type');
		$subjectid = I('subjectid');
		if (!$num) {
			$data = $this->return_array("1", "请输入优惠码");
		} else {
			$data = Logic("BoxEdu", "User")->check_card($num, $type, $subjectid);

		}
		echo json_encode($data);
	}

	/**********购买课程***/
	public function order() {
		//	dump(I());exit();
		$school_id = $this->user_info['school_id'];
		$school_num = trim(I('school_num'));
		$subjectid = I('subjectid', "0", "intval");
		$examination_id = I('examination_id', "0", "intval");
		$param = array(
			'school_id' => $school_id,
			'subjectid' => $subjectid,
		);

		if (!intval($school_id)) {
			exit(json_encode(array('error' => 6, 'msg' => '请先填写学校信息', 'user_info' => $this->user_info)));
		}

		$r = Logic("BoxEdu", "User")->getPid($this->user_id);
		$p_user_id = $r['school_pid'];
		if ($p_user_id) {
			$this->parent_user = BoxModel("edu_user")->where(array('user_id' => $p_user_id))->find();
			if ($this->parent_user) {
				$school_num = $this->parent_user['school_num'];
			}
		}
		$offline_subject = Logic("BoxEdu", "Subject")->get_offline_subject_price($param);
		if ($offline_subject['offline'] == 3) {
			$select_offline = 1;
		} else {
			$select_offline = I('offline_subject_id', 0, "intval");
		}
		if ($offline_subject['apply'] == 3 && !$examination_id) {
			exit(json_encode(array('error' => 5, 'msg' => '请先填写填名信息')));
		}

		$data = array(
			'subjectid' => $subjectid,
			'offline_subject_ids' => $select_offline,
			'card_num' => I('card_num'),
			'school_num' => $school_num,
			// 'agent_num' => I('agent_num'),
			'use_yie' => is_numeric(I('yie')) ? I('yie') : 0,
			'type' => 1,
			'examination_id' => $examination_id,
		);
		$return = Logic("BoxEdu", "User")->buy_subject($data);
		echo json_encode($return);
	}

	/****选课程*/
	public function main() {
		$subjectid = I('subjectid');
		$this->data = BoxModel("EduSubject")->load($subjectid);
		$my_rank = BoxModel('EduSubjectRank')->where("user_id='$this->user_id' and subjectid='$subjectid'")->find();
		$this->my_num = $my_num = $my_rank ? $my_rank['num'] : 0; //我的积分
		$my_rank_num = BoxModel('EduSubjectRank')->where("num>'$my_num' and subjectid='$subjectid'")->count(); //我的排名
		$this->my_rank_num = ($my_rank_num + 1);
		/*****学习进度****/
		$this->learning_pre = Logic("BoxEdu", "User")->learning_pre($subjectid);
		$this->display();
	}

	/****选课程章节*/
	public function sections() {
		$subjectid = I('subjectid');
		$this->data = BoxModel("EduSubject")->load($subjectid);
		$this->display();
	}

	/****章节详细*/
	public function detail() {
		$this->tab = I('tab', 'section');
		$this->knowsid = $knowsid = I('knowsid');
		$this->knows_data = BoxModel("edu_knows")->load($knowsid);
		$subjectid = $this->subjectid = $this->knows_data['section']['subjectid'];
		$has_buy = Logic("BoxEdu", "Subject")->has_buy($subjectid, $this->user_id);
		if ($has_buy['error'] == 1) {
			$this->jump("请先开通课程", U('Subject/buy', array('subjectid' => $subjectid)));
		}
		if ($has_buy['error'] == 2) {
			$this->jump("", U('Subject/try_out', array('subjectid' => $subjectid)));
		}
		$this->sectionid = $this->knows_data['section']['sectionid'];
		//if($this->tab=="exercise"){
		$data = BoxModel("edu_exams")->where("knowsid=$knowsid and examstatus=1")->find();
		$this->examid = $data['examid'];
		///dump($this->str);exit();
		$this->display();
	}

	/*******需要正在开通***/
	public function try_out() {
		$this->subjectid = I('subjectid');
		$this->display();
	}

	/*****保存笔记****/
	public function save_note() {
		$row = array(
			'notes' => I('notes'),
			'knowsid' => intval(I('knowsid')),
			'user_id' => $this->user_id,
			'updatetime' => date("Y-m-d H:i:s"),
		);
		$model = BoxModel("edu_subject_notes");
		$where = array('user_id' => $this->user_id, 'knowsid' => $row['knowsid']);
		$f = $model->where($where)->find();
		if ($f) {
			$id = $model->where($where)->save($row);
		} else {
			$id = $model->add($row);
		}
		echo $id;

	}

	/****全真模拟考试*/
	public function moni() {
		$this->subjectid = I('subjectid');
		$this->data = BoxModel("edu_subject")->load($subjectid);
		$this->where = "subjectid='$subjectid' and examtype in ('autopage','selfpage') and examstatus=1";
		$this->display();
	}

	/****全真模拟考试*/
	public function moni_info_show() {
		$this->header_nav = "Public:none";
		$this->data = Logic("BoxEdu", "Exam")->marking(I());
		$this->examid = I('examid');
		$exams_data = json_decode($this->data['exams_data'], true);
		$this->getQuestype($exams_data['examsetting']);
		$this->display();
	}

	/****全真模拟考试*/
	public function moni_jiexi() {
		$this->header_nav = "Public:none";
		$this->type = I('type');
		$this->examid = I('examid');
		$this->data = Logic("BoxEdu", "Exam")->load($this->examid);
		//	$this->answer=$this->data['marking']['right_answer']
		//dump($this->data);
		//$exams_data=json_decode($this->data['exams_data'],true);
		$this->getQuestype($this->data['examsetting']);
		$this->display();
	}

	/****选课程*/
	public function video() {
		$this->display();
	}

	/*****学霸榜**/
	public function xueba() {
		$this->class_list = BoxModel("EduSubject")->subject_class();
		$this->display();
	}

	/*****学霸榜**/
	public function xueba_info() {
		// dump($this->user_id);
		$subjectid = I('subjectid');
		$this->subject_info = BoxModel('EduSubject')->load($subjectid);
		$my_rank = BoxModel('EduSubjectRank')->where("user_id='$this->user_id' and subjectid='$subjectid'")->find();
		$this->my_num = $my_num = $my_rank ? $my_rank['num'] : 0; //我的积分
		$my_rank_num = BoxModel('EduSubjectRank')->where("num>'$my_num' and subjectid='$subjectid'")->count(); //我的排名
		$data = BoxModel('EduSubjectRank')->get("subjectid='$subjectid'", 20, "num desc");
		$this->my_rank_num = $my_rank_num > 19 ? "未上榜" : ($my_rank_num + 1);
		$this->data = $data['list'];
		$this->display();
	}

	//选择试题
	public function select_exercises() {
		$sectionid = I('sections_id');
		$this->data = BoxModel("edu_exams")->get(array('sectionid' => $sectionid, 'examstatus' => 1, 'examtype' => 'module_exam'));
		$this->sectionData = BoxModel("edu_sections")->where(array('sectionid' => $sectionid))->find();
		$this->display();
	}

	//选择课程
	public function select_subject() {
		$subjectid = I('subjectid');
		$this->data = BoxModel("edu_subject")->get(array('parent_id' => $subjectid), "", "order_by");
		$this->display();
	}

	//考试
	public function moni_into() {
		$this->header_nav = "Public:none";
		$this->exam_id = I('examid');
		$this->data = BoxModel("edu_exams")->where(array('examid' => $this->exam_id))->find();
		$this->display();
	}

	//做题记录
	public function dotistatis() {
		$this->subject_nav = "tiku";
		$this->display();
	}

	//提问
	public function question_detail() {
		$this->qid = I('qid');
		$this->form = array(
			'pid' => I('pid', "0", "intval"),
			'pid2' => I('pid2', "0", "intval"),
			'pid3' => I('pid3', "0", "intval"),
			'pid4' => I('qid', "0", "intval"),
		);
		$this->questionsData = BoxModel("edu_questions")->load($this->qid);
		$this->title = cutstr($this->questionsData['question'], 100);
		$this->display();
	}

	//错题练习
	public function lookinto() {
		$this->header_nav = "Public:none";
		$model = BoxModel('edu_questions_user');
		$type = I('type', "wrong");
		$where = array(
			'user_id' => $this->user_info['user_id'],
			'type' => $type,
		);
		if ($sections_id = I('sections_id')) {
			$data = BoxModel("edu_sections")->where(array('sectionid' => $sections_id))->find();
			$this->title = $data['section'];
			$where['sections_id'] = $sections_id;
		}
		if ($examid = I('examid')) {
			$data = BoxModel("edu_exams")->where(array('examid' => $examid))->find();
			$this->title = $data['exam'];
			$where['exams_id'] = $examid;
		}

		$data = $model->get($where, 10);
		$this->count_wrong_all = intval($data['count']) - 10;
		$this->show_page($data['pager']);
		$questionsModel = BoxModel("edu_questions");
		$questions = array();
		foreach ($data['list'] as $one) {
			$questions[] = $questionsModel->load($one['questions_id']);
		}
		//dump($questions);
		$this->questions = $questions;
		$this->display();
	}

	/*******结算页面**/
	public function checkout() {
		$this->buy_type = I('type');
		$subjectid = I('subjectid');
		$this->subject_info = BoxModel("edu_subject")->load($subjectid);
		$show_price = $this->subject_info['price'];
		/*******线下课程**/
		$school_id = $this->user_info['school_id'];
		$param = array(
			'school_id' => $school_id,
			'subjectid' => $subjectid,
		);
		$r = Logic("BoxEdu", "User")->getPid($this->user_id);
		$p_user_id = $r['school_pid'];
		if ($p_user_id) {
			$this->parent_user = BoxModel("edu_user")->where(array('user_id' => $p_user_id))->find();
		}
		$this->offline_subject = Logic("BoxEdu", "Subject")->get_offline_subject_price($param);
		//	dump($this->offline_subject);
		if ($this->offline_subject['offline'] == 3) {
			$this->select_offline = 3;
			$show_price += $this->offline_subject['price'];
		} else {
			$this->select_offline = I('select_offline', 0, "intval");
		}
		if ($this->offline_subject['apply'] == 3) {
			$this->select_apply = 3;
			$show_price += $this->offline_subject['examination_fee'];
		} else {
			$this->select_apply = I('select_apply', 0, "intval"); //
		}
		$this->show_price = $show_price;
		$this->buy_count = BoxModel("edu_subject_order")->where(array('subjectid' => $subjectid, 'status' => array('in', "2,4")))->count();

		//dump( $this->select_offline);
		$this->display();
	}

	public function offline_guide() {
		$this->display();
	}

	/*******视频播放位置,看完视频给积分*****/
	public function user_play_video() {
		$where = array(
			'knowsid' => I('knowsid', "0", "intval"),
			'user_id' => $this->user_info['user_id'],
		);
		if (I('knowsid', "0", "intval") && $this->user_info['user_id']) {
			$time = I('time', "0", "intval");
			$knowsData = BoxModel("edu_knows")->load($where['knowsid']);
			$longtime = intval($knowsData['video_info']['duration']);
			$model = BoxModel("edu_knows_user");
			$percent = intval(($time / $longtime) * 100);
			$type = I('type', "0", "intval");
			$data = $model->where($where)->find();
			if ($data) {

				if ($type == 2) {
					//看完
					$percent = 100;
				}

				if ($time > $longtime - 6) {
					//误差6秒为看完
					$type = 2;
					$percent = 100;
				}
				$row = array(
					'percent' => $percent,
					'type' => $type,
					'subjectid' => $knowsData['subjectid'],
					'add_time' => date("Y-m-d H:i:s"),
				);
				if ($type != 2) {
					unset($row['type']);
				}
				$row['play_time'] = $time;
				$model->where($where)->save($row);
				if ($data['type'] != 2 && $type == 2) {
//看完视频获得经验值
					$row = array(
						'user_id' => $this->user_info['user_id'],
						'num' => $knowsData['xuebazi'],
						'order_sn' => "",
						'title' => $knowsData['knows'],
						'url' => HomeUrl . U('Subject/lesson_detail', array('knowsid' => $knowsData['knowsid'], 'subject_id' => $knowsData['subjectid'])),
						'subjectid' => $knowsData['subjectid'],
						'event' => '00006',
					);
					//dump($row);
					$r = Logic('BoxEdu', 'User')->add_xuebazi($row);
				}
			} else {
				$where['percent'] = $percent;
				$model->add($where);
			}
		}

	}

}